﻿using EasyModbus;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace RFIDLibrary
{

    
    public class RFID_Class
    {

        [Serializable]
        public class Data_class
        {
            public int Count { get; set; } = 0;
            public DateTime date { get; set; }
        }

        Data_class data_Class = new Data_class();

        private int address_A { get; set; } = 0x24;
        private int address_B { get; set; } = 0x40;


        private static ModbusClient _modbusClient = new ModbusClient();

        public RFID_Class()
        {
        }

        #region //外部调用
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="Port">端口</param>
        /// <param name="Bandrate">波特率</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFID_init(string Port, int Bandrate, out string mes, byte busStation = 2)
        {
            try
            {
                if (_modbusClient == null)
                {
                    mes = "错误，端口打开失败。_modbusClient：未实例化";
                    RFIDLogInfo(mes);
                    return 2;
                }

                if (_modbusClient.Connected)
                {
                    _modbusClient.Disconnect();
                }

                _modbusClient.SerialPort = Port;
                _modbusClient.UnitIdentifier = busStation;
                _modbusClient.Baudrate = Bandrate;
                _modbusClient.Parity = System.IO.Ports.Parity.None;
                _modbusClient.StopBits = System.IO.Ports.StopBits.One;
                _modbusClient.ConnectionTimeout = 500;
                _modbusClient.Connect();


                mes = "";
                RFIDLogInfo("端口打开成功。");
                return 0;
            }
            catch (Exception er)
            {
                mes = "错误，端口打开失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }


        /// <summary>
        /// 释放端口
        /// </summary>
        /// <param name="msg"></param>
        /// <returns>0成功，1失败</returns>
        public int RFID_Dispose(out string mes)
        {
            try
            {
                if (_modbusClient == null)
                {
                    mes = "错误，端口关闭失败。_modbusClient：未实例化";
                    RFIDLogInfo(mes);
                    return 2;
                }

                _modbusClient.Disconnect();
                mes = "";
                RFIDLogInfo("错误，端口关闭成功。");
                return 0;
            }
            catch (Exception er)
            {
                mes = "错误，端口关闭失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 校验RFID
        /// </summary>
        /// <param name="Station">工位P1-P10</param>
        /// <param name="Mode">产品机型</param>
        /// <param name="Check_state">返回校验状态</param>
        /// <param name="RFID_value">返回RFID</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFID_Check(string Station, string Mode, out bool[] Check_state, out string[] RFID_value, out string mes)
        {
            string[] rfid = new string[2];
            try
            {
                //获取工位RFID
                RFIDLogInfo("开始读取RFID...");
                Thread.Sleep(80);
                int rFID_a = RFID_Read(address_A, out rfid, out mes);
                if (rFID_a != 0)
                {
                    if (rFID_a != 0)
                        mes = "工位RFID读取错误 校验失败：" + mes;
                    Check_state = new bool[] { false, false };
                    RFID_value = new string[] { "", "" };
                    RFIDLogInfo(mes);
                    return 2;
                }
                RFIDLogInfo("读取RFID完成...");

                //校验RFID是否重复校验
                if (!Check2(rfid, out mes))
                {
                    Check_state = new bool[] {false,false };
                    RFID_value = rfid;
                    mes += "错误，工位A与B穴位校验失败，码重复。";
                    RFIDLogInfo(mes);
                    return 2;
                }

                if (Station.IndexOf("P1") >= 0)
                {
                    bool[] buf = new bool[] { false, false };
                    buf[0] = Check3(Station, Mode, rfid[0], out mes);
                    buf[1] = Check3(Station, Mode, rfid[1], out mes);
                    //前缀规则校验

                    if (!buf[0] || !buf[1])
                    {
                        RFID_value = rfid;
                        Check_state = new bool[2] { buf[0], buf[1] };
                        if (!buf[0])
                            mes = "错误，工位" + Station + "A穴位校验失败。";
                        if (!buf[1])
                            mes += "错误，工位" + Station + "B穴位校验失败。";

                        RFIDLogInfo(mes);
                        return 0;
                    }


                    RFID_value = rfid;
                    Check_state = new bool[] { true, true };
                    mes = "";
                    RFIDLogInfo("工位：" + Station + " RFID_A:" + rfid[0] + " RFID_B:" + rfid[1] + " 校验成功。");
                    return 0;
                }
                else
                {
                    //校验RFID
                    bool state_a = Check(Station, Mode, rfid[0], out mes);
                    bool state_b = Check(Station, Mode, rfid[1], out mes);


                    //匹配本站条码返回失败
                    if (!Check2(Station, rfid[0]))
                    {
                        state_a = false;
                        mes = "错误，工位" + Station + "A穴位已经安装。";
                        RFIDLogInfo(mes);
                    }
                    if (!Check2(Station, rfid[1]))
                    {
                        state_b = false;
                        mes = "错误，工位" + Station + "B穴位已经安装。";
                        RFIDLogInfo(mes);
                    }

                    if (!state_a || !state_b)
                    {
                        RFID_value = rfid;
                        Check_state = new bool[2] { state_a, state_b };
                        if (!state_a)
                            mes = "错误，工位" + Station + "A穴位校验失败。";
                        if (!state_b)
                            mes += "错误，工位" + Station + "B穴位校验失败。";

                        RFIDLogInfo(mes);
                        return 0;
                    }


                    string[] rfid_buf = new string[] { "", "" };
                    try
                    {
                        rfid_buf[0] = rfid[0].Substring(0, GetStationLength(Station)-2);
                        rfid_buf[1] = rfid[1].Substring(0, GetStationLength(Station)-2);
                        RFID_value = rfid_buf;
                    }
                    catch { rfid_buf = rfid; }

                    RFID_value = rfid_buf;
                    Check_state = new bool[2] { state_a, state_b };
                    mes = "";
                    RFIDLogInfo("工位：" + Station + " RFID_A:" + rfid[0] + " RFID_B:" + rfid[1] + " 校验成功。");
                    return 0;
                }
            }
            catch (Exception er)
            {
                RFID_value = new string[] { "", "" };
                Check_state = new bool[] { false, false };
                mes = "错误，工位" + Station + "校验失败; " + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 写RFID
        /// </summary>
        /// <param name="Station">工位P1、P2、P4、P5、P6、P7、P8、P10</param>
        /// <param name="Check_state">元素0=A,元素1=B，写入结果OK、NG、P、R、N、空格为不写</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFIDState_Write(string Station, string[] Check_state, out string[] _value, out string mes)
        {
            try
            {
                RFIDLogInfo("开始写RFID...");
                string date_str1 = "";
                string date_str2 = "";
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    _value = new string[] { "", "" };
                    mes = "错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return 2;
                }
                if (Check_state == null || Check_state[0] == null || Check_state[1] == null)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入状态异常";
                    RFIDLogInfo(mes);
                    return 3;
                }
                Thread.Sleep(50);
                switch (Station)
                {
                    case "P1":
                        #region //P1 RFID写入

                        //计数读与保存取
                        try { data_Class = Deserialize<Data_class>(); } catch { data_Class = new Data_class(); }
                        DateTime t1 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
                        DateTime t2 = DateTime.Parse(data_Class.date.ToString("yyyy-MM-dd"));
                        //DateTime t2 = DateTime.Parse("2024-05-25");
                        double hhhh = DiffDays(t1, t2);
                        if (hhhh > 0)
                        {
                            data_Class.Count = 0;
                        }

                        date_str1 = DateTime.Now.ToString("yyMMdd");
                        date_str2 = DateTime.Now.ToString("yyMMdd");
                        data_Class.Count++;
                        date_str1 += data_Class.Count.ToString("0000");
                        data_Class.Count++;
                        date_str2 += data_Class.Count.ToString("0000");

                        //计数保存
                        data_Class.date = DateTime.Now;
                        Serialize(data_Class);

                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P1"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O1"; break;
                            case "R": date_str1 += "R1"; break;
                            case "N": date_str1 += "N1"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 += "P1"; break;
                            case "NG": date_str2 += "NG"; break;
                            case "O": date_str2 += "O1"; break;
                            case "R": date_str2 += "R1"; break;
                            case "N": date_str2 += "N1"; break;
                        }

                        //开始写入
                        string mes1 = "";
                        string mes2 = "";
                        mes = "";
                        date_str1 += "                     ";//22个空字符
                        if (RFID_Write(0x26, date_str1, out mes1) != 0)
                        {
                            mes += mes1 + "错误，工位P1 A穴位写入失败。";
                            RFIDLogInfo(mes);
                        }
                        date_str2 += "                     ";//22个空字符
                        Thread.Sleep(80);
                        if (RFID_Write(0x42, date_str2, out mes2) != 0)
                        {
                            mes += mes2 + "错误，工位P1 B穴位写入失败。";
                            RFIDLogInfo(mes);
                        }
                        ;
                        break;
                    #endregion
                    case "P2":
                        #region //P2 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P2"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O2"; break;
                            case "R": date_str1 = "R2"; break;
                            case "N": date_str1 = "N2"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P2"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O2"; break;
                            case "R": date_str2 = "R2"; break;
                            case "N": date_str2 = "N2"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x2C, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P2 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P2 A穴位为空或NG未写入。"); }

                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x48, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P2 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P2 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P4":
                        #region //P4 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P4"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O4"; break;
                            case "R": date_str1 = "R4"; break;
                            case "N": date_str1 = "N4"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P4"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O4"; break;
                            case "R": date_str2 = "R4"; break;
                            case "N": date_str2 = "N4"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x2E, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P4 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P4 A穴位为空或NG未写入。"); }

                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4A, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P4 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P4 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P5":
                        #region //P5 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P5"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O5"; break;
                            case "R": date_str1 = "R5"; break;
                            case "N": date_str1 = "N5"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P5"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O5"; break;
                            case "R": date_str2 = "R5"; break;
                            case "N": date_str2 = "N5"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x2F, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P5 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P5 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4B, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P5 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P5 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P6":
                        #region //P6 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P6"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O6"; break;
                            case "R": date_str1 = "R6"; break;
                            case "N": date_str1 = "N6"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P6"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O6"; break;
                            case "R": date_str2 = "R6"; break;
                            case "N": date_str2 = "N6"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x30, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P6 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P6 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4C, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P6 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P6 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P7":
                        #region //P7 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P7"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O7"; break;
                            case "R": date_str1 = "R7"; break;
                            case "N": date_str1 = "N7"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P7"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O7"; break;
                            case "R": date_str2 = "R7"; break;
                            case "N": date_str2 = "N7"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x31, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P7 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P7 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4D, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P7 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P7 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P8":
                        #region //P8 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P8"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O8"; break;
                            case "R": date_str1 += "R8"; break;
                            case "N": date_str1 += "N8"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P8"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O8"; break;
                            case "R": date_str2 = "R8"; break;
                            case "N": date_str2 = "N8"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x32, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P8 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P8 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4E, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P8 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P8 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P10":
                        #region //P8 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P10"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O10"; break;
                            case "R": date_str1 = "R10"; break;
                            case "N": date_str1 = "N10"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P10"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O10"; break;
                            case "R": date_str2 = "R10"; break;
                            case "N": date_str2 = "N10"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x34, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P10 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P10 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x50, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P10 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P10 B穴位为空或NG未写入。"); }
                        ;
                        break;
                        #endregion
                }

                //读取RFID
                Thread.Sleep(100);
                string[] rfid_buf = new string[] { "", "" };
                int rFID_a = RFID_Read(address_A, out rfid_buf, out mes);
                if (rFID_a != 0)
                {
                    mes += "错误，工位" + Station +  "读RFID错误：" + mes;
                    _value = new string[] { "", "" };
                    RFIDLogInfo(mes);
                    return 4;
                }

                string[] rfid_buf2 = new string[] { "", "" };
                try
                {
                    rfid_buf2[0] = rfid_buf[0].Substring(0, GetStationLength(Station));
                    rfid_buf2[1] = rfid_buf[1].Substring(0, GetStationLength(Station));
                }
                catch { rfid_buf2 = rfid_buf; }

                _value = rfid_buf2;

                mes = "工位:" + Station + " A写RFID成功。" + _value[0] + "\r\n" + "工位:" + Station + " B写RFID成功。" + _value[1];
                RFIDLogInfo("工位:" + Station + " A写RFID成功。" + _value[0]);
                RFIDLogInfo("工位:" + Station + " B写RFID成功。" + _value[1]);
                return 0;
            }
            catch (Exception er)
            {
                _value = new string[] { "", "" };
                mes = "错误，工位" + Station + "写RFID失败; " + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 写RFID
        /// </summary>
        /// <param name="Station">工位P3、P9</param>
        /// <param name="Check_state">元素0=A,元素1=B，写入结果OK、NG、P、R、N</param>
        /// <param name="RFID_value">写入前的RFID</param>
        /// <param name="_value">返回写入后的RFID</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFIDState_Write(string Station, string[] Check_state, string[] RFID_value, out string[] _value, out string mes)
        {
            try
            {
                RFIDLogInfo("开始写RFID...");
                string date_str1 = "";
                string date_str2 = "";
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return 2;
                }

                //传入结果判断
                if (Check_state == null || Check_state.Length <= 0)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入无状态";
                    RFIDLogInfo(mes);
                    return 3;
                }
                if (Check_state[0] == null || Check_state[1] == null)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入无状态";
                    RFIDLogInfo(mes);
                    return 3;
                }

                //传入条码判断
                if (RFID_value == null || RFID_value.Length <= 0)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入无条码";
                    RFIDLogInfo(mes);
                    return 3;
                }

                //
                if (RFID_value[0].Contains("NG"))
                {
                    RFID_value[0] = "";
                    Check_state[0] = " ";
                }
                if (RFID_value[1].Contains("NG"))
                {
                    RFID_value[1] = "";
                    Check_state[1] = " ";
                }

                //校验RFID是否重复校验
                if (!Check4(RFID_value, out mes))
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误重复，工位A与B穴位校验失败。";
                    RFIDLogInfo(mes);
                    return 4;
                }


                //校验RFID
                mes = "";
                string mes1 = "";
                string mes2 = "";
                bool state_a = false;
                bool state_b = false;
                 state_a = Check(Station, RFID_value[0], out mes1);
                 state_b = Check(Station, RFID_value[1], out mes2);
                if (!state_a || !state_b)
                {
                    if (!state_a)
                        mes = mes1 + " 写RFID错误，工位" + Station + "  A穴位RFID校验失败。";
                    if (!state_b)
                        mes += mes1 + " 写RFID错误，工位" + Station + " B穴位RFID校验失败。";
                    if (!state_a && !state_b)
                    {
                        _value = new string[] { "", "" };
                        RFIDLogInfo(mes);
                        return 5;
                    }
                }

                string[] RFID_SN = new string[2];
                switch (Station)
                {
                    case "P3":
                        #region //P3 RFID写入
                        //生成新的ID
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                            date_str1 = RFID_value[0].Substring(0, GetStationLength(Station) - 2).Substring(4);
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                            date_str2 = RFID_value[1].Substring(0, GetStationLength(Station) - 2).Substring(4);
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P3"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O3"; break;
                            case "R": date_str1 += "R3"; break;
                            case "N": date_str1 += "N3"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 += "P3"; break;
                            case "NG": date_str2 += "NG"; break;
                            case "O": date_str2 += "O3"; break; 
                            case "R": date_str2 += "R3"; break;
                            case "N": date_str2 += "N3"; break;
                        }

                        //开始写入
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write(0x26, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P3 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P3 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write(0x42, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P3 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P3 B穴位为空或NG未写入。"); }

                        break;
                    #endregion

                    case "P9":
                        #region //P3 RFID写入
                        //生成新的ID
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                            date_str1 = RFID_value[0].Substring(0, GetStationLength(Station) - 2).Substring(4);
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                            date_str2 = RFID_value[1].Substring(0, GetStationLength(Station) - 2).Substring(4);

                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P9"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O9"; break;
                            case "R": date_str1 += "R9"; break;
                            case "N": date_str1 += "N9"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 += "P9"; break;
                            case "NG": date_str2 += "NG"; break;
                            case "O": date_str2 += "O9"; break;
                            case "R": date_str2 += "R9"; break;
                            case "N": date_str2 += "N9"; break;
                        }

                        //开始写入
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            if (RFID_Write(0x26, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P9 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P9 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write(0x42, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P9 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位9 B穴位为空或NG未写入。"); }
                        break;
                        #endregion
                }

                //读取RFID
                Thread.Sleep(100);
                string[] rfid_buf = new string[] { "", "" };
                int rFID_a = RFID_Read(address_A, out rfid_buf, out mes);
                if (rFID_a != 0)
                {
                    mes += "错误,工位: " + Station + "读RFID错误：" + mes;
                    _value = new string[] { "", "" };
                    RFIDLogInfo(mes);
                    return 3;
                }

                string[] rfid_buf2 = new string[] { "", "" };
                try
                {
                    rfid_buf2[0] = rfid_buf[0].Substring(0, GetStationLength(Station));
                    rfid_buf2[1] = rfid_buf[1].Substring(0, GetStationLength(Station));
                }
                catch { rfid_buf2 = rfid_buf; }

                _value = rfid_buf2;
                mes = "";
                RFIDLogInfo("工位:" + Station + " A写RFID成功。" + date_str1);
                RFIDLogInfo("工位:" + Station + " B写RFID成功。" + date_str2);
                return 0;
            }
            catch (Exception er)
            {
                _value = new string[] { "", "" };
                mes = "错误，工位" + Station + "失败; " + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        #endregion



        #region //内部调用
        /// <summary>
        /// 获取所有RFID
        /// </summary>
        /// <param name="Rfid_str"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private int RFID_Read(int address, out string Rfid_value, out string mes)
        {
            byte[] str_buf = new byte[36];
            try
            {
                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    str_buf = _modbusClient.ReadHoldingRegisters_byte(address, 18);
                    if (str_buf != null && str_buf.Length == 36)
                    {
                        break;
                    }
                }

                if (str_buf != null && str_buf.Length == 36)
                {
                    int count = 0;
                    for (int i = 0; i < str_buf.Length; i++)
                    {
                        if (str_buf[i] <= 0)
                        {
                            count++;
                            if (count > 9)
                            {
                                Rfid_value = "";
                                mes = "RFID读取失败！未检测到标签";
                                RFIDLogInfo(mes);
                                return 2;
                            }
                        }

                    }
                    char[] str = Array.ConvertAll(str_buf, c => Convert.ToChar(c));
                    //Rfid_str =  string.Concat<char>(str).Trim('\0');
                    Rfid_value = new string(str).Trim('\0');

                    if (Rfid_value == string.Empty)
                    {
                        Rfid_value = "";
                        mes = "RFID读取失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }
                    mes = "";
                    RFIDLogInfo("读取RFID成功B：" + Rfid_value);
                    return 0;
                }
                else
                {
                    Rfid_value = "";
                    mes = "RFID读取失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
            }
            catch (Exception er)
            {
                Rfid_value = "";
                mes = "RFID读取失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 获取所有RFID
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Rfid_value"></param>
        /// <param name="mes"></param>
        /// <returns></returns>
        public int RFID_Read(int address, out string[] Rfid_value, out string mes)
        {
            byte[] str_buf = new byte[112];
            byte[] str_buf2 = new byte[37];
            byte[] str_buf3 = new byte[37];
            try
            {
                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    str_buf = _modbusClient.ReadHoldingRegisters_byte(address, 50);
                    if (str_buf != null && str_buf.Length == 100)
                    {
                        break;
                    }
                    Thread.Sleep(100);
                }

                if (str_buf != null && str_buf.Length == 100)
                {
                    int count = 0;
                    for (int i = 0; i < str_buf.Length; i++)
                    {
                        if (str_buf[i] <= 0)
                        {
                            count++;
                            if (count > 10)
                            {
                                Rfid_value = new string[] {"","" };
                                mes = "RFID读取失败！未检测到标签";
                                RFIDLogInfo(mes);
                                return 2;
                            }
                        }

                    }

                    Array.Copy(str_buf, 0, str_buf2, 0, 36);
                    Array.Copy(str_buf, 56, str_buf3, 0, 36);
                    char[] str = Array.ConvertAll(str_buf2, c => Convert.ToChar(c));
                    char[] str2 = Array.ConvertAll(str_buf3, c => Convert.ToChar(c));
                    //Rfid_str =  string.Concat<char>(str).Trim('\0');

                    Rfid_value = new string[2];
                    Rfid_value[0] = new string(str).Trim('\0').Replace("\0", "").Replace(" ", "").Replace("\r", "").Replace("\n", "");
                    Rfid_value[1] = new string(str2).Trim('\0').Replace("\0", "").Replace(" ", "").Replace("\r", "").Replace("\n", "");

                    if (Rfid_value[0] == string.Empty || Rfid_value[1] == string.Empty)
                    {
                        Rfid_value = new string[] { "", "" };
                        mes = "RFID读取失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }

                    mes = "";
                    RFIDLogInfo("读取RFID成功A：" + Rfid_value[0]);
                    RFIDLogInfo("读取RFID成功B：" + Rfid_value[1]);
                    return 0;
                }
                else
                {
                    Rfid_value = new string[] { "", "" };
                    mes = "RFID读取失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
            }
            catch (Exception er)
            {
                Rfid_value = new string[] { "", "" };
                mes = "RFID读取失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// RFID写入
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Rfid_value"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private int RFID_Write(int address, string Rfid_value, out string mes)
        {
            byte[] hhhh = new byte[1];
            try
            {
                //字符转换
                char[] buf = Rfid_value.ToCharArray();
                int[] str_value = new int[buf.Length / 2];
                for (int i = 0; i < str_value.Length; i++)
                {
                    str_value[i] = (buf[i * 2] << 8) | buf[i * 2 + 1];
                }
                ;
                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        hhhh = _modbusClient.WriteMultipleRegisters(address, str_value);
                        if (hhhh != null && hhhh.Length > 0)
                        {
                            if (hhhh[1] != 0x90)
                            {
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    catch (Exception er)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }

                }

                if (hhhh == null)
                {
                    mes = "RFID写入失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
                else
                {
                    if (hhhh.Length < 0)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }
                    else
                    {
                        if (hhhh[1] == 0x90)
                        {
                            mes = "RFID写入失败！,未检测到标签";
                            RFIDLogInfo(mes);
                            return 2;
                        }
                    }

                }

                mes = "";
                RFIDLogInfo("RFID写入成功：" + address +"_"+ Rfid_value);
                return 0;
            }
            catch (Exception er)
            {
                mes = "RFID写入失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        public int RFID_Write2(int address, string Rfid_value, out string mes)
        {
            byte[] hhhh = new byte[1];
            try
            {
                //字符转换
                char[] buf = Rfid_value.ToCharArray();
                short str_value =(short)((buf[0] << 8) | buf[1]);
                ;

                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        hhhh = _modbusClient.WriteSingleRegister_byte(address, str_value);
                        if (hhhh != null && hhhh.Length > 0)
                        {
                            if (hhhh[1] != 0x90)
                            {
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    catch (Exception er)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }

                }

                if (hhhh == null)
                {
                    mes = "RFID写入失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
                else
                {
                    if (hhhh.Length < 0)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }
                    else
                    {
                        if (hhhh[1] == 0x90)
                        {
                            mes = "RFID写入失败！,未检测到标签";
                            RFIDLogInfo(mes);
                            return 2;
                        }
                    }

                }

                //


                mes = "";
                RFIDLogInfo("RFID写入成功：" + address + "_" + Rfid_value);
                return 0;
            }
            catch (Exception er)
            {
                mes = "RFID写入失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }


        private bool Check(string Station, string Mode, string RFID_value, out string mes)
        {
            string Date_str = "";
            string Station_data = "";
            try
            {
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    mes = "错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return false;
                }

                //判断RFID长度
                if (RFID_value == string.Empty)
                {
                    mes = "错误，工位" + Station + " RFID为空或。";
                    RFIDLogInfo(mes);
                    return false;
                }
                try
                {
                    RFID_value = RFID_value.Substring(0, GetStationLength(Station) - 2);
                }
                catch
                {
                    //NG匹配
                    int pos2 = RFID_value.IndexOf("NG");
                    if (pos2 >= 0)
                    {
                        mes = "错误，工位" + GetStation(pos2 - 14) + "状态为NG。";
                        RFIDLogInfo(mes);
                        return false;
                    }
                }

                //前缀匹配
                Date_str = RFID_value.Substring(0, 14);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，工位" + Station + "RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }


                //机型匹配
                var parrern2 = @"^(?=.{1}$).*$";
                if (!Regex.IsMatch(Mode, parrern2))
                {
                    mes = "错误，工位" + Station + "输入的机型不符合规则。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!RFID_value.Substring(0, 1).Contains(Mode))
                {
                    mes = "错误，工位" + Station + "机型匹配不正确。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //NG匹配
                int pos = RFID_value.IndexOf("NG");
                if (pos >= 0)
                {
                    mes = "错误，工位" + GetStation(pos - 14) + "状态为NG。";
                    RFIDLogInfo(mes);
                    return false;
                }
                ;

                //规则判断
                #region 
                string parrern3 = "";
                Station_data = RFID_value.Substring(14);
                switch (Station)
                {
                    case "P1": break;
                    case "P2":
                        parrern3 = @"^(O|P|R|N)1$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P2:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P3":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P3:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P4":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P4:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P5":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P5:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P6":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P6:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P7":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P7:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P8":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P8:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P9":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P9:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P10":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8+(O|P|R|N)9$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P10:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                }
                #endregion

                mes = "";
                RFIDLogInfo("工位" + Station + "基本校验成功。" + RFID_value);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误，工位" + Station + ":" + RFID_value + "基本校验失败," + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }
        private bool Check(string Station, string RFID_value, out string mes)
        {
            string Date_str = "";
            string Station_data = "";
            try
            {
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    mes = "错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return false;
                }

                //判断RFID长度
                if (RFID_value == string.Empty)
                {
                    mes = "错误，工位" + Station + " RFID为空或。";
                    RFIDLogInfo(mes);
                    return false;
                }
                try
                {
                    RFID_value = RFID_value.Substring(0, GetStationLength(Station) - 2);
                }
                catch
                {
                    //NG匹配
                    int pos2 = RFID_value.IndexOf("NG");
                    if (pos2 >= 0)
                    {
                        mes = "错误，工位" + GetStation(pos2 - 14) + "状态为NG。";
                        RFIDLogInfo(mes);
                        return false;
                    }
                }

                //前缀匹配
                Date_str = RFID_value.Substring(0, 14);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，工位" + Station + "RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //规则判断
                #region 
                string parrern3 = "";
                Station_data = RFID_value.Substring(14);
                switch (Station)
                {
                    case "P1": break;
                    case "P2":
                        parrern3 = @"^(O|P|R|N)1$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P2:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P3":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P3:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P4":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P4:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P5":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P5:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P6":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P6:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P7":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P7:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P8":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P8:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P9":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P9:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P10":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8+(O|P|R|N)9$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P10:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                }
                #endregion

                mes = "";
                RFIDLogInfo("工位" + Station + "基本校验成功。" + RFID_value);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误，工位" + Station + ":" + RFID_value + "基本校验失败," + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }

        //是否NG
        private bool Check2(string Station, string RFID_value)
        {
                string mes = "";
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    return false;
                }

                try
                {
                    RFID_value = RFID_value.Substring(GetStationLength(Station) - 2,2);
                    parrern = @"^(O|P|N|R)(10|[1-9])$";
                    if (Regex.IsMatch(RFID_value, parrern))
                    {
                        return true;
                    }
                    if (RFID_value.Contains("NG"))
                    {
                        return false;
                    }
                    return true;
                }
                catch
                {
                    return true;
                }

        }

        private bool Check3(string Station,string Mode, string RFID_value, out string mes)
        {
            try
            {
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    mes = "错误，工位" + Station + "前缀不符合规则，请检查。"; ;
                    return false;
                }

                //前缀匹配
                RFID_value = RFID_value.Substring(0, 14);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(RFID_value, parrern1))
                {
                    mes = "错误，工位" + Station + "RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    mes = "";
                    return false;
                }

                //机型匹配
                var parrern2 = @"^(?=.{1}$).*$";
                if (!Regex.IsMatch(Mode, parrern2))
                {
                    mes = "错误，工位" + Station + "输入的机型不符合规则。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!RFID_value.Substring(0, 1).Contains(Mode))
                {
                    mes = "错误，工位" + Station + "机型匹配不正确。";
                    RFIDLogInfo(mes);
                    return false;
                }

                mes = "";
                return true;
            }
            catch (Exception er)
            {
                mes = "错误，工位" + Station + ":" + RFID_value + "基本校验失败," + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                return false;
            }
        }


        /// <summary>
        /// AB穴位RFID码是否重复校验
        /// </summary>
        /// <param name="Rfid_value"></param>
        /// <param name="mes"></param>
        /// <returns></returns>
        private bool Check2(string[] Rfid_value, out string mes)
        {
            try
            {

                if (Rfid_value == null || Rfid_value.Length <= 0)
                {
                    mes = "错误，传入的RFID为空，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //单个码不判断重复
                if (Rfid_value[0] == string.Empty || Rfid_value[1] == string.Empty)
                {
                    mes = "有码为空不判断重复";
                    RFIDLogInfo(mes);
                    return true;
                }

                //前缀匹配
                string Date_str = Rfid_value[0].Substring(0, 14);
                string Date_str2 = Rfid_value[1].Substring(0, 14);
                RFIDLogInfo("A穴位：" + Date_str);
                RFIDLogInfo("B穴位：" + Date_str2);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，穴位A:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!Regex.IsMatch(Date_str2, parrern1))
                {
                    mes = "错误，穴位B:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //检查2个RFID是否重复
                if (Rfid_value[0] == Rfid_value[1])
                {
                    mes = "错误，穴位A与B:RFID重复，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //检查2个RFID是否重复
                string str1 = "";
                string str2 = "";
                if (Rfid_value[0] != string.Empty)
                    str1 = Rfid_value[0].Substring(3, 1);
                if (Rfid_value[0] != string.Empty)
                    str2 = Rfid_value[1].Substring(3, 1);
                if (Rfid_value[0] != string.Empty && str1 != "A")
                {
                    mes = "错误，穴位A：字母不匹配或为反异常，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (Rfid_value[1] != string.Empty && str2 != "B")
                {
                    mes = "错误，穴位B：字母不匹配或为反异常，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                mes = "";
                RFIDLogInfo("重码校验成功。A_RFID" + Rfid_value[0]);
                RFIDLogInfo("重码校验成功。B_RFID" + Rfid_value[1]);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误,重码校验失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }

        private bool Check4(string[] Rfid_value, out string mes)
        {
            try
            {

                if (Rfid_value == null || Rfid_value.Length <= 0)
                {
                    mes = "错误，传入的RFID为空，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //单个码不判断重复
                if (Rfid_value[0] == string.Empty || Rfid_value[1] == string.Empty)
                {
                    mes = "有码为空不判断重复";
                    RFIDLogInfo(mes);
                    return true;
                }

                //前缀匹配
                string Date_str = Rfid_value[0].Substring(0, 14);
                string Date_str2 = Rfid_value[1].Substring(0, 14);
                RFIDLogInfo("A穴位：" + Date_str);
                RFIDLogInfo("B穴位：" + Date_str2);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，穴位A:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!Regex.IsMatch(Date_str2, parrern1))
                {
                    mes = "错误，穴位B:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //检查2个RFID是否重复
                if (Rfid_value[0] == Rfid_value[1])
                {
                    mes = "错误，穴位A与B:RFID重复，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                ////检查2个RFID是否重复
                //string str1 = "";
                //string str2 = "";
                //if (Rfid_value[0] != string.Empty)
                //    str1 = Rfid_value[0].Substring(3, 1);
                //if (Rfid_value[0] != string.Empty)
                //    str2 = Rfid_value[1].Substring(3, 1);
                //if (Rfid_value[0] != string.Empty && str1 != "A")
                //{
                //    mes = "错误，穴位A：字母不匹配或为反异常，请检查。";
                //    RFIDLogInfo(mes);
                //    return false;
                //}
                //if (Rfid_value[1] != string.Empty && str2 != "B")
                //{
                //    mes = "错误，穴位B：字母不匹配或为反异常，请检查。";
                //    RFIDLogInfo(mes);
                //    return false;
                //}

                mes = "";
                RFIDLogInfo("重码校验成功。A_RFID" + Rfid_value[0]);
                RFIDLogInfo("重码校验成功。B_RFID" + Rfid_value[1]);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误,重码校验失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }

        private int GetStationLength(string Station)
        {
            try
            {
                switch (Station)
                {
                    case "P1": return 16;
                    case "P2": return 18;
                    case "P3": return 20;
                    case "P4": return 22;
                    case "P5": return 24;
                    case "P6": return 26;
                    case "P7": return 28;
                    case "P8": return 30;
                    case "P9": return 32;
                    case "P10": return 34;
                    default: return 0;
                }
            }
            catch (Exception)
            {
                return 0;
            }

        }
        private string GetStation(int pos)
        {
            try
            {
                switch (pos)
                {
                    case 0: return "P1";
                    case 2: return "P2";
                    case 4: return "P3";
                    case 6: return "P4";
                    case 8: return "P5";
                    case 10: return "P6";
                    case 12: return "P7";
                    case 14: return "P8";
                    case 16: return "P9";
                    case 18: return "P10";
                    default: return "";
                }
            }
            catch (Exception)
            {
                return "";
            }

        }

        public double DiffDays(DateTime startTime, DateTime endTime)
        {
            TimeSpan daysSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return daysSpan.TotalDays;
        }

        #endregion


        #region 信息

        static object logInfoLock = new object();
        static string path = "D://Logs/RFIDLog/";
        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="strMessage">信息</param>
        private static void RFIDLogInfo(string strMessage)
        {
            try
            {
                lock (logInfoLock)
                {
                    strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "->" + strMessage.Replace("\r\n", "");
                    strMessage += "\r\n";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    StreamWriter LogSw = new StreamWriter(path + DateTime.Today.ToString("yyyy-MM-dd") + ".txt", true, Encoding.GetEncoding(0));
                    LogSw.WriteLine(strMessage);
                    LogSw.Flush();
                    LogSw.Close();
                    LogSw.Dispose();
                }
            }
            catch
            {

            }
        }
        #endregion


        #region
        public void Serialize<T>(T source)//序列化方法
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            if (Object.ReferenceEquals(source, null))
            {
                return;
            }

            using (FileStream fs = new FileStream("D:\\Config\\Person.adc", FileMode.Create))//创建一个文件流，对文件进行写入
            {
                BinaryFormatter bf = new BinaryFormatter();//使用CLR二进制格式器
                bf.Serialize(fs, source); //序列化到硬盘
                fs.Close();//关闭文件流
            }
        }

        public T Deserialize<T>()//反序列
        {

            using (FileStream fs = new FileStream("D:\\Config\\Person.adc", FileMode.Open))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                return (T)binaryFormatter.Deserialize(fs);
            }
        }

        public T Deserialize2<T>()//夸集合反序列
        {

            using (FileStream fs = new FileStream("D:\\Config\\Person.adc", FileMode.Open))
            {
                IFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Binder = new UBinder();
                return (T)binaryFormatter.Deserialize(fs);
            }
        }

        public class UBinder : SerializationBinder
        {
            public override Type BindToType(string assemblyName, string typeName)
            {
                Assembly ass = Assembly.GetExecutingAssembly();
                return ass.GetType(typeName);
            }
        }

        #endregion

    }


    public class RFID_Class2
    {

        [Serializable]
        public class Data_class
        {
            public int Count { get; set; } = 0;
            public DateTime date { get; set; }
        }

        Data_class data_Class = new Data_class();

        private int address_A { get; set; } = 0x24;
        private int address_B { get; set; } = 0x40;


        private static ModbusClient _modbusClient2 = new ModbusClient();

        public RFID_Class2()
        {
        }

        #region //外部调用
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="Port">端口</param>
        /// <param name="Bandrate">波特率</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFID_init(string Port, int Bandrate, out string mes, byte busStation = 2)
        {
            try
            {
                if (_modbusClient2 == null)
                {
                    mes = "错误，端口打开失败。_modbusClient：未实例化";
                    RFIDLogInfo(mes);
                    return 2;
                }

                if (_modbusClient2.Connected)
                {
                    _modbusClient2.Disconnect();
                }

                _modbusClient2.SerialPort = Port;
                _modbusClient2.UnitIdentifier = busStation;
                _modbusClient2.Baudrate = Bandrate;
                _modbusClient2.Parity = System.IO.Ports.Parity.None;
                _modbusClient2.StopBits = System.IO.Ports.StopBits.One;
                _modbusClient2.ConnectionTimeout = 500;
                _modbusClient2.Connect();


                mes = "";
                RFIDLogInfo("端口打开成功。");
                return 0;
            }
            catch (Exception er)
            {
                mes = "错误，端口打开失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }


        /// <summary>
        /// 释放端口
        /// </summary>
        /// <param name="msg"></param>
        /// <returns>0成功，1失败</returns>
        public int RFID_Dispose(out string mes)
        {
            try
            {
                if (_modbusClient2 == null)
                {
                    mes = "错误，端口关闭失败。_modbusClient：未实例化";
                    RFIDLogInfo(mes);
                    return 2;
                }

                _modbusClient2.Disconnect();
                mes = "";
                RFIDLogInfo("错误，端口关闭成功。");
                return 0;
            }
            catch (Exception er)
            {
                mes = "错误，端口关闭失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }


        /// <summary>
        /// 校验RFID
        /// </summary>
        /// <param name="Station">工位P1-P10</param>
        /// <param name="Mode">产品机型</param>
        /// <param name="Check_state">返回校验状态</param>
        /// <param name="RFID_value">返回RFID</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFID_Check(string Station, string Mode, out bool[] Check_state, out string[] RFID_value, out string mes)
        {
            string[] rfid = new string[2];
            try
            {
                //获取工位RFID
                RFIDLogInfo("开始读取RFID...");
                Thread.Sleep(80);
                int rFID_a = RFID_Read(address_A, out rfid, out mes);
                if (rFID_a != 0)
                {
                    if (rFID_a != 0)
                        mes = "工位RFID读取错误 校验失败：" + mes;
                    Check_state = new bool[] { false, false };
                    RFID_value = new string[] { "", "" };
                    RFIDLogInfo(mes);
                    return 2;
                }
                RFIDLogInfo("读取RFID完成...");

                //校验RFID是否重复校验
                if (!Check2(rfid, out mes))
                {
                    Check_state = new bool[] { false, false };
                    RFID_value = rfid;
                    mes += "错误，工位A与B穴位校验失败，码重复。";
                    RFIDLogInfo(mes);
                    return 2;
                }

                if (Station.IndexOf("P1") >= 0)
                {
                    bool[] buf = new bool[] { false, false };
                    buf[0] = Check3(Station, Mode, rfid[0], out mes);
                    buf[1] = Check3(Station, Mode, rfid[1], out mes);
                    //前缀规则校验

                    if (!buf[0] || !buf[1])
                    {
                        RFID_value = rfid;
                        Check_state = new bool[2] { buf[0], buf[1] };
                        if (!buf[0])
                            mes = "错误，工位" + Station + "A穴位校验失败。";
                        if (!buf[1])
                            mes += "错误，工位" + Station + "B穴位校验失败。";

                        RFIDLogInfo(mes);
                        return 0;
                    }


                    RFID_value = rfid;
                    Check_state = new bool[] { true, true };
                    mes = "";
                    RFIDLogInfo("工位：" + Station + " RFID_A:" + rfid[0] + " RFID_B:" + rfid[1] + " 校验成功。");
                    return 0;
                }
                else
                {
                    //校验RFID
                    bool state_a = Check(Station, Mode, rfid[0], out mes);
                    bool state_b = Check(Station, Mode, rfid[1], out mes);


                    //匹配本站条码返回失败
                    if (!Check2(Station, rfid[0]))
                    {
                        state_a = false;
                        mes = "错误，工位" + Station + "A穴位已经安装。";
                        RFIDLogInfo(mes);
                    }
                    if (!Check2(Station, rfid[1]))
                    {
                        state_b = false;
                        mes = "错误，工位" + Station + "B穴位已经安装。";
                        RFIDLogInfo(mes);
                    }

                    if (!state_a || !state_b)
                    {
                        RFID_value = rfid;
                        Check_state = new bool[2] { state_a, state_b };
                        if (!state_a)
                            mes = "错误，工位" + Station + "A穴位校验失败。";
                        if (!state_b)
                            mes += "错误，工位" + Station + "B穴位校验失败。";

                        RFIDLogInfo(mes);
                        return 0;
                    }


                    string[] rfid_buf = new string[] { "", "" };
                    try
                    {
                        rfid_buf[0] = rfid[0].Substring(0, GetStationLength(Station) - 2);
                        rfid_buf[1] = rfid[1].Substring(0, GetStationLength(Station) - 2);
                        RFID_value = rfid_buf;
                    }
                    catch { rfid_buf = rfid; }

                    RFID_value = rfid_buf;
                    Check_state = new bool[2] { state_a, state_b };
                    mes = "";
                    RFIDLogInfo("工位：" + Station + " RFID_A:" + rfid[0] + " RFID_B:" + rfid[1] + " 校验成功。");
                    return 0;
                }
            }
            catch (Exception er)
            {
                RFID_value = new string[] { "", "" };
                Check_state = new bool[] { false, false };
                mes = "错误，工位" + Station + "校验失败; " + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 写RFID
        /// </summary>
        /// <param name="Station">工位P1、P2、P4、P5、P6、P7、P8、P10</param>
        /// <param name="Check_state">元素0=A,元素1=B，写入结果OK、NG、P、R、N、空格为不写</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFIDState_Write(string Station, string[] Check_state, out string[] _value, out string mes)
        {
            try
            {
                RFIDLogInfo("开始写RFID...");
                string date_str1 = "";
                string date_str2 = "";
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    _value = new string[] { "", "" };
                    mes = "错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return 2;
                }
                if (Check_state == null || Check_state[0] == null || Check_state[1] == null)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入状态异常";
                    RFIDLogInfo(mes);
                    return 3;
                }
                Thread.Sleep(50);
                switch (Station)
                {
                    case "P1":
                        #region //P1 RFID写入

                        //计数读与保存取
                        try { data_Class = Deserialize<Data_class>(); } catch { data_Class = new Data_class(); }
                        DateTime t1 = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
                        DateTime t2 = DateTime.Parse(data_Class.date.ToString("yyyy-MM-dd"));
                        //DateTime t2 = DateTime.Parse("2024-05-25");
                        double hhhh = DiffDays(t1, t2);
                        if (hhhh > 0)
                        {
                            data_Class.Count = 0;
                        }

                        date_str1 = DateTime.Now.ToString("yyMMdd");
                        date_str2 = DateTime.Now.ToString("yyMMdd");
                        data_Class.Count++;
                        date_str1 += data_Class.Count.ToString("0000");
                        data_Class.Count++;
                        date_str2 += data_Class.Count.ToString("0000");

                        //计数保存
                        data_Class.date = DateTime.Now;
                        Serialize(data_Class);

                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P1"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O1"; break;
                            case "R": date_str1 += "R1"; break;
                            case "N": date_str1 += "N1"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 += "P1"; break;
                            case "NG": date_str2 += "NG"; break;
                            case "O": date_str2 += "O1"; break;
                            case "R": date_str2 += "R1"; break;
                            case "N": date_str2 += "N1"; break;
                        }

                        //开始写入
                        string mes1 = "";
                        string mes2 = "";
                        mes = "";
                        date_str1 += "                     ";//22个空字符
                        if (RFID_Write(0x26, date_str1, out mes1) != 0)
                        {
                            mes += mes1 + "错误，工位P1 A穴位写入失败。";
                            RFIDLogInfo(mes);
                        }
                        date_str2 += "                     ";//22个空字符
                        Thread.Sleep(80);
                        if (RFID_Write(0x42, date_str2, out mes2) != 0)
                        {
                            mes += mes2 + "错误，工位P1 B穴位写入失败。";
                            RFIDLogInfo(mes);
                        }
                        ;
                        break;
                    #endregion
                    case "P2":
                        #region //P2 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P2"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O2"; break;
                            case "R": date_str1 = "R2"; break;
                            case "N": date_str1 = "N2"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P2"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O2"; break;
                            case "R": date_str2 = "R2"; break;
                            case "N": date_str2 = "N2"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x2C, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P2 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P2 A穴位为空或NG未写入。"); }

                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x48, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P2 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P2 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P4":
                        #region //P4 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P4"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O4"; break;
                            case "R": date_str1 = "R4"; break;
                            case "N": date_str1 = "N4"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P4"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O4"; break;
                            case "R": date_str2 = "R4"; break;
                            case "N": date_str2 = "N4"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x2E, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P4 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P4 A穴位为空或NG未写入。"); }

                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4A, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P4 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P4 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P5":
                        #region //P5 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P5"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O5"; break;
                            case "R": date_str1 = "R5"; break;
                            case "N": date_str1 = "N5"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P5"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O5"; break;
                            case "R": date_str2 = "R5"; break;
                            case "N": date_str2 = "N5"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x2F, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P5 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P5 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4B, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P5 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P5 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P6":
                        #region //P6 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P6"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O6"; break;
                            case "R": date_str1 = "R6"; break;
                            case "N": date_str1 = "N6"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P6"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O6"; break;
                            case "R": date_str2 = "R6"; break;
                            case "N": date_str2 = "N6"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x30, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P6 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P6 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4C, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P6 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P6 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P7":
                        #region //P7 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P7"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O7"; break;
                            case "R": date_str1 = "R7"; break;
                            case "N": date_str1 = "N7"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P7"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O7"; break;
                            case "R": date_str2 = "R7"; break;
                            case "N": date_str2 = "N7"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x31, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P7 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P7 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4D, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P7 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P7 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P8":
                        #region //P8 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P8"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O8"; break;
                            case "R": date_str1 += "R8"; break;
                            case "N": date_str1 += "N8"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P8"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O8"; break;
                            case "R": date_str2 = "R8"; break;
                            case "N": date_str2 = "N8"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x32, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P8 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P8 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x4E, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P8 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P8 B穴位为空或NG未写入。"); }
                        ;
                        break;
                    #endregion
                    case "P10":
                        #region //P8 RFID写入
                        date_str1 = "";
                        date_str2 = "";
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 = "P10"; break;
                            case "NG": date_str1 = "NG"; break;
                            case "O": date_str1 = "O10"; break;
                            case "R": date_str1 = "R10"; break;
                            case "N": date_str1 = "N10"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 = "P10"; break;
                            case "NG": date_str2 = "NG"; break;
                            case "O": date_str2 = "O10"; break;
                            case "R": date_str2 = "R10"; break;
                            case "N": date_str2 = "N10"; break;
                        }

                        //开始写入
                        mes = "";
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write2(0x34, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P10 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P10 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write2(0x50, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P10 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P10 B穴位为空或NG未写入。"); }
                        ;
                        break;
                        #endregion
                }

                //读取RFID
                Thread.Sleep(100);
                string[] rfid_buf = new string[] { "", "" };
                int rFID_a = RFID_Read(address_A, out rfid_buf, out mes);
                if (rFID_a != 0)
                {
                    mes += "错误，工位" + Station + "读RFID错误：" + mes;
                    _value = new string[] { "", "" };
                    RFIDLogInfo(mes);
                    return 4;
                }

                string[] rfid_buf2 = new string[] { "", "" };
                try
                {
                    rfid_buf2[0] = rfid_buf[0].Substring(0, GetStationLength(Station));
                    rfid_buf2[1] = rfid_buf[1].Substring(0, GetStationLength(Station));
                }
                catch { rfid_buf2 = rfid_buf; }

                _value = rfid_buf2;

                mes = "工位:" + Station + " A写RFID成功。" + _value[0] + "\r\n" + "工位:" + Station + " B写RFID成功。" + _value[1];
                RFIDLogInfo("工位:" + Station + " A写RFID成功。" + _value[0]);
                RFIDLogInfo("工位:" + Station + " B写RFID成功。" + _value[1]);
                return 0;
            }
            catch (Exception er)
            {
                _value = new string[] { "", "" };
                mes = "错误，工位" + Station + "写RFID失败; " + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 写RFID
        /// </summary>
        /// <param name="Station">工位P3、P9</param>
        /// <param name="Check_state">元素0=A,元素1=B，写入结果OK、NG、P、R、N</param>
        /// <param name="RFID_value">写入前的RFID</param>
        /// <param name="_value">返回写入后的RFID</param>
        /// <param name="mes"></param>
        /// <returns>0成功，1失败</returns>
        public int RFIDState_Write(string Station, string[] Check_state, string[] RFID_value, out string[] _value, out string mes)
        {
            try
            {
                RFIDLogInfo("开始写RFID...");
                string date_str1 = "";
                string date_str2 = "";
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return 2;
                }

                //传入结果判断
                if (Check_state == null || Check_state.Length <= 0)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入无状态";
                    RFIDLogInfo(mes);
                    return 3;
                }
                if (Check_state[0] == null || Check_state[1] == null)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入无状态";
                    RFIDLogInfo(mes);
                    return 3;
                }

                //传入条码判断
                if (RFID_value == null || RFID_value.Length <= 0)
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误，输入无条码";
                    RFIDLogInfo(mes);
                    return 3;
                }

                //
                if (RFID_value[0].Contains("NG"))
                {
                    RFID_value[0] = "";
                    Check_state[0] = " ";
                }
                if (RFID_value[1].Contains("NG"))
                {
                    RFID_value[1] = "";
                    Check_state[1] = " ";
                }

                //校验RFID是否重复校验
                if (!Check4(RFID_value, out mes))
                {
                    _value = new string[] { "", "" };
                    mes = "写工位" + Station + "RFID错误重复，工位A与B穴位校验失败。";
                    RFIDLogInfo(mes);
                    return 4;
                }


                //校验RFID
                mes = "";
                string mes1 = "";
                string mes2 = "";
                bool state_a = false;
                bool state_b = false;
                state_a = Check(Station, RFID_value[0], out mes1);
                state_b = Check(Station, RFID_value[1], out mes2);
                if (!state_a || !state_b)
                {
                    if (!state_a)
                        mes = mes1 + " 写RFID错误，工位" + Station + "  A穴位RFID校验失败。";
                    if (!state_b)
                        mes += mes1 + " 写RFID错误，工位" + Station + " B穴位RFID校验失败。";
                    if (!state_a && !state_b)
                    {
                        _value = new string[] { "", "" };
                        RFIDLogInfo(mes);
                        return 5;
                    }
                }

                string[] RFID_SN = new string[2];
                switch (Station)
                {
                    case "P3":
                        #region //P3 RFID写入
                        //生成新的ID
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                            date_str1 = RFID_value[0].Substring(0, GetStationLength(Station) - 2).Substring(4);
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                            date_str2 = RFID_value[1].Substring(0, GetStationLength(Station) - 2).Substring(4);
                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P3"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O3"; break;
                            case "R": date_str1 += "R3"; break;
                            case "N": date_str1 += "N3"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 += "P3"; break;
                            case "NG": date_str2 += "NG"; break;
                            case "O": date_str2 += "O3"; break;
                            case "R": date_str2 += "R3"; break;
                            case "N": date_str2 += "N3"; break;
                        }

                        //开始写入
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                        {
                            if (RFID_Write(0x26, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P3 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P3 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write(0x42, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P3 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P3 B穴位为空或NG未写入。"); }

                        break;
                    #endregion

                    case "P9":
                        #region //P3 RFID写入
                        //生成新的ID
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                            date_str1 = RFID_value[0].Substring(0, GetStationLength(Station) - 2).Substring(4);
                        if (Check_state[0] != string.Empty && Check_state[0] != " ")
                            date_str2 = RFID_value[1].Substring(0, GetStationLength(Station) - 2).Substring(4);

                        //A
                        switch (Check_state[0])
                        {
                            case "OK": date_str1 += "P9"; break;
                            case "NG": date_str1 += "NG"; break;
                            case "O": date_str1 += "O9"; break;
                            case "R": date_str1 += "R9"; break;
                            case "N": date_str1 += "N9"; break;
                        }
                        //B
                        switch (Check_state[1])
                        {
                            case "OK": date_str2 += "P9"; break;
                            case "NG": date_str2 += "NG"; break;
                            case "O": date_str2 += "O9"; break;
                            case "R": date_str2 += "R9"; break;
                            case "N": date_str2 += "N9"; break;
                        }

                        //开始写入
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            if (RFID_Write(0x26, date_str1, out mes1) != 0)
                            {
                                mes += mes1 + "错误，工位P9 A穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位P9 A穴位为空或NG未写入。"); }
                        if (Check_state[1] != string.Empty && Check_state[1] != " ")
                        {
                            Thread.Sleep(80);
                            if (RFID_Write(0x42, date_str2, out mes2) != 0)
                            {
                                mes += mes2 + "错误，工位P9 B穴位写入失败。";
                                RFIDLogInfo(mes);
                            }
                        }
                        else { RFIDLogInfo("工位9 B穴位为空或NG未写入。"); }
                        break;
                        #endregion
                }

                //读取RFID
                Thread.Sleep(100);
                string[] rfid_buf = new string[] { "", "" };
                int rFID_a = RFID_Read(address_A, out rfid_buf, out mes);
                if (rFID_a != 0)
                {
                    mes += "错误,工位: " + Station + "读RFID错误：" + mes;
                    _value = new string[] { "", "" };
                    RFIDLogInfo(mes);
                    return 3;
                }

                string[] rfid_buf2 = new string[] { "", "" };
                try
                {
                    rfid_buf2[0] = rfid_buf[0].Substring(0, GetStationLength(Station));
                    rfid_buf2[1] = rfid_buf[1].Substring(0, GetStationLength(Station));
                }
                catch { rfid_buf2 = rfid_buf; }

                _value = rfid_buf2;
                mes = "";
                RFIDLogInfo("工位:" + Station + " A写RFID成功。" + date_str1);
                RFIDLogInfo("工位:" + Station + " B写RFID成功。" + date_str2);
                return 0;
            }
            catch (Exception er)
            {
                _value = new string[] { "", "" };
                mes = "错误，工位" + Station + "失败; " + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        #endregion



        #region //内部调用
        /// <summary>
        /// 获取所有RFID
        /// </summary>
        /// <param name="Rfid_str"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private int RFID_Read(int address, out string Rfid_value, out string mes)
        {
            byte[] str_buf = new byte[36];
            try
            {
                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    str_buf = _modbusClient2.ReadHoldingRegisters_byte(address, 18);
                    if (str_buf != null && str_buf.Length == 36)
                    {
                        break;
                    }
                }

                if (str_buf != null && str_buf.Length == 36)
                {
                    int count = 0;
                    for (int i = 0; i < str_buf.Length; i++)
                    {
                        if (str_buf[i] <= 0)
                        {
                            count++;
                            if (count > 9)
                            {
                                Rfid_value = "";
                                mes = "RFID读取失败！未检测到标签";
                                RFIDLogInfo(mes);
                                return 2;
                            }
                        }

                    }
                    char[] str = Array.ConvertAll(str_buf, c => Convert.ToChar(c));
                    //Rfid_str =  string.Concat<char>(str).Trim('\0');
                    Rfid_value = new string(str).Trim('\0');

                    if (Rfid_value == string.Empty)
                    {
                        Rfid_value = "";
                        mes = "RFID读取失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }
                    mes = "";
                    RFIDLogInfo("读取RFID成功B：" + Rfid_value);
                    return 0;
                }
                else
                {
                    Rfid_value = "";
                    mes = "RFID读取失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
            }
            catch (Exception er)
            {
                Rfid_value = "";
                mes = "RFID读取失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// 获取所有RFID
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Rfid_value"></param>
        /// <param name="mes"></param>
        /// <returns></returns>
        public int RFID_Read(int address, out string[] Rfid_value, out string mes)
        {
            byte[] str_buf = new byte[112];
            byte[] str_buf2 = new byte[37];
            byte[] str_buf3 = new byte[37];
            try
            {
                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    str_buf = _modbusClient2.ReadHoldingRegisters_byte(address, 50);
                    if (str_buf != null && str_buf.Length == 100)
                    {
                        break;
                    }
                    Thread.Sleep(800);
                }

                if (str_buf != null && str_buf.Length == 100)
                {
                    int count = 0;
                    for (int i = 0; i < str_buf.Length; i++)
                    {
                        if (str_buf[i] <= 0)
                        {
                            count++;
                            if (count > 10)
                            {
                                Rfid_value = new string[] { "", "" };
                                mes = "RFID读取失败！未检测到标签";
                                RFIDLogInfo(mes);
                                return 2;
                            }
                        }

                    }

                    Array.Copy(str_buf, 0, str_buf2, 0, 36);
                    Array.Copy(str_buf, 56, str_buf3, 0, 36);
                    char[] str = Array.ConvertAll(str_buf2, c => Convert.ToChar(c));
                    char[] str2 = Array.ConvertAll(str_buf3, c => Convert.ToChar(c));
                    //Rfid_str =  string.Concat<char>(str).Trim('\0');

                    Rfid_value = new string[2];
                    Rfid_value[0] = new string(str).Trim('\0').Replace("\0", "").Replace(" ", "").Replace("\r", "").Replace("\n", "");
                    Rfid_value[1] = new string(str2).Trim('\0').Replace("\0", "").Replace(" ", "").Replace("\r", "").Replace("\n", "");

                    if (Rfid_value[0] == string.Empty || Rfid_value[1] == string.Empty)
                    {
                        Rfid_value = new string[] { "", "" };
                        mes = "RFID读取失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }

                    mes = "";
                    RFIDLogInfo("读取RFID成功A：" + Rfid_value[0]);
                    RFIDLogInfo("读取RFID成功B：" + Rfid_value[1]);
                    return 0;
                }
                else
                {
                    Rfid_value = new string[] { "", "" };
                    mes = "RFID读取失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
            }
            catch (Exception er)
            {
                Rfid_value = new string[] { "", "" };
                mes = "RFID读取失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        /// <summary>
        /// RFID写入
        /// </summary>
        /// <param name="address"></param>
        /// <param name="Rfid_value"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        private int RFID_Write(int address, string Rfid_value, out string mes)
        {
            byte[] hhhh = new byte[1];
            try
            {
                //字符转换
                char[] buf = Rfid_value.ToCharArray();
                int[] str_value = new int[buf.Length / 2];
                for (int i = 0; i < str_value.Length; i++)
                {
                    str_value[i] = (buf[i * 2] << 8) | buf[i * 2 + 1];
                }
                ;
                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        hhhh = _modbusClient2.WriteMultipleRegisters(address, str_value);
                        if (hhhh != null && hhhh.Length > 0)
                        {
                            if (hhhh[1] != 0x90)
                            {
                                break;
                            }
                        }
                        Thread.Sleep(800);
                    }
                    catch (Exception er)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }

                }

                if (hhhh == null)
                {
                    mes = "RFID写入失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
                else
                {
                    if (hhhh.Length < 0)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }
                    else
                    {
                        if (hhhh[1] == 0x90)
                        {
                            mes = "RFID写入失败！,未检测到标签";
                            RFIDLogInfo(mes);
                            return 2;
                        }
                    }

                }

                mes = "";
                RFIDLogInfo("RFID写入成功：" + address + "_" + Rfid_value);
                return 0;
            }
            catch (Exception er)
            {
                mes = "RFID写入失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }

        private int RFID_Write2(int address, string Rfid_value, out string mes)
        {
            byte[] hhhh = new byte[1];
            try
            {
                //字符转换
                char[] buf = Rfid_value.ToCharArray();
                short str_value = (short)((buf[0] << 8) | buf[1]);
                ;

                //失败将会重发3次
                for (int i = 0; i < 3; i++)
                {
                    try
                    {
                        hhhh = _modbusClient2.WriteSingleRegister_byte(address, str_value);
                        if (hhhh != null && hhhh.Length > 0)
                        {
                            if (hhhh[1] != 0x90)
                            {
                                break;
                            }
                        }
                        Thread.Sleep(100);
                    }
                    catch (Exception er)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }

                }

                if (hhhh == null)
                {
                    mes = "RFID写入失败！";
                    RFIDLogInfo(mes);
                    return 2;
                }
                else
                {
                    if (hhhh.Length < 0)
                    {
                        mes = "RFID写入失败！";
                        RFIDLogInfo(mes);
                        return 2;
                    }
                    else
                    {
                        if (hhhh[1] == 0x90)
                        {
                            mes = "RFID写入失败！,未检测到标签";
                            RFIDLogInfo(mes);
                            return 2;
                        }
                    }

                }

                mes = "";
                RFIDLogInfo("RFID写入成功：" + address + "_" + Rfid_value);
                return 0;
            }
            catch (Exception er)
            {
                mes = "RFID写入失败！" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return 1;
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="Station"></param>
        /// <param name="Mode"></param>
        /// <param name="RFID_value"></param>
        /// <param name="mes"></param>
        /// <returns></returns>
        private bool Check(string Station, string Mode, string RFID_value, out string mes)
        {
            string Date_str = "";
            string Station_data = "";
            try
            {
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    mes = "错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return false;
                }

                //判断RFID长度
                if (RFID_value == string.Empty)
                {
                    mes = "错误，工位" + Station + " RFID为空或。";
                    RFIDLogInfo(mes);
                    return false;
                }
                try
                {
                    RFID_value = RFID_value.Substring(0, GetStationLength(Station) - 2);
                }
                catch
                {
                    //NG匹配
                    int pos2 = RFID_value.IndexOf("NG");
                    if (pos2 >= 0)
                    {
                        mes = "错误，工位" + GetStation(pos2 - 14) + "状态为NG。";
                        RFIDLogInfo(mes);
                        return false;
                    }
                }

                //前缀匹配
                Date_str = RFID_value.Substring(0, 14);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，工位" + Station + "RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }


                //机型匹配
                var parrern2 = @"^(?=.{1}$).*$";
                if (!Regex.IsMatch(Mode, parrern2))
                {
                    mes = "错误，工位" + Station + "输入的机型不符合规则。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!RFID_value.Substring(0, 1).Contains(Mode))
                {
                    mes = "错误，工位" + Station + "机型匹配不正确。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //NG匹配
                int pos = RFID_value.IndexOf("NG");
                if (pos >= 0)
                {
                    mes = "错误，工位" + GetStation(pos - 14) + "状态为NG。";
                    RFIDLogInfo(mes);
                    return false;
                }
                ;

                //规则判断
                #region 
                string parrern3 = "";
                Station_data = RFID_value.Substring(14);
                switch (Station)
                {
                    case "P1": break;
                    case "P2":
                        parrern3 = @"^(O|P|R|N)1$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P2:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P3":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P3:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P4":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P4:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P5":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P5:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P6":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P6:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P7":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P7:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P8":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P8:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P9":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P9:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P10":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8+(O|P|R|N)9$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P10:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                }
                #endregion

                mes = "";
                RFIDLogInfo("工位" + Station + "基本校验成功。" + RFID_value);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误，工位" + Station + ":" + RFID_value + "基本校验失败," + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }
        private bool Check(string Station, string RFID_value, out string mes)
        {
            string Date_str = "";
            string Station_data = "";
            try
            {
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    mes = "错误，输入的工位号不符合规则";
                    RFIDLogInfo(mes);
                    return false;
                }

                //判断RFID长度
                if (RFID_value == string.Empty)
                {
                    mes = "错误，工位" + Station + " RFID为空或。";
                    RFIDLogInfo(mes);
                    return false;
                }
                try
                {
                    RFID_value = RFID_value.Substring(0, GetStationLength(Station) - 2);
                }
                catch
                {
                    //NG匹配
                    int pos2 = RFID_value.IndexOf("NG");
                    if (pos2 >= 0)
                    {
                        mes = "错误，工位" + GetStation(pos2 - 14) + "状态为NG。";
                        RFIDLogInfo(mes);
                        return false;
                    }
                }

                //前缀匹配
                Date_str = RFID_value.Substring(0, 14);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，工位" + Station + "RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //规则判断
                #region 
                string parrern3 = "";
                Station_data = RFID_value.Substring(14);
                switch (Station)
                {
                    case "P1": break;
                    case "P2":
                        parrern3 = @"^(O|P|R|N)1$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P2:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P3":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P3:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P4":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P4:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P5":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P5:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P6":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P6:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P7":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P7:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P8":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P8:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P9":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P9:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                    case "P10":
                        parrern3 = @"^(O|P|R|N)1+(O|P|R|N)2+(O|P|R|N)3+(O|P|R|N)4+(O|P|R|N)5+(O|P|R|N)6+(O|P|R|N)7+(O|P|R|N)8+(O|P|R|N)9$";
                        if (!Regex.IsMatch(Station_data, parrern3))
                        {
                            mes = "错误，工位P10:RFID不符合规则" + Station_data;
                            RFIDLogInfo(mes);
                            return false;
                        }
                        break;
                }
                #endregion

                mes = "";
                RFIDLogInfo("工位" + Station + "基本校验成功。" + RFID_value);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误，工位" + Station + ":" + RFID_value + "基本校验失败," + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }

        //是否有本站工位号
        private bool Check2(string Station, string RFID_value)
        {
            string mes = "";
            //判断站号P1-P10
            var parrern = @"^(P|P)(10|[1-9])$";
            if (!Regex.IsMatch(Station, parrern))
            {
                return false;
            }

            try
            {
                RFID_value = RFID_value.Substring(GetStationLength(Station) - 2, 2);
                parrern = @"^(O|P|N|R)(10|[1-9])$";
                if (Regex.IsMatch(RFID_value, parrern))
                {
                    return true;
                }
                if (RFID_value.Contains("NG"))
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return true;
            }
        }
        private bool Check3(string Station, string Mode, string RFID_value, out string mes)
        {
            try
            {
                //判断站号P1-P10
                var parrern = @"^(P|P)(10|[1-9])$";
                if (!Regex.IsMatch(Station, parrern))
                {
                    mes = "错误，工位" + Station + "前缀不符合规则，请检查。"; ;
                    return false;
                }

                //前缀匹配
                RFID_value = RFID_value.Substring(0, 14);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(RFID_value, parrern1))
                {
                    mes = "错误，工位" + Station + "RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    mes = "";
                    return false;
                }

                //机型匹配
                var parrern2 = @"^(?=.{1}$).*$";
                if (!Regex.IsMatch(Mode, parrern2))
                {
                    mes = "错误，工位" + Station + "输入的机型不符合规则。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!RFID_value.Substring(0, 1).Contains(Mode))
                {
                    mes = "错误，工位" + Station + "机型匹配不正确。";
                    RFIDLogInfo(mes);
                    return false;
                }

                mes = "";
                return true;
            }
            catch (Exception er)
            {
                mes = "错误，工位" + Station + ":" + RFID_value + "基本校验失败," + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                return false;
            }
        }

        /// <summary>
        /// AB穴位RFID码是否重复校验
        /// </summary>
        /// <param name="Rfid_value"></param>
        /// <param name="mes"></param>
        /// <returns></returns>
        private bool Check2(string[] Rfid_value, out string mes)
        {
            try
            {
                if (Rfid_value == null || Rfid_value.Length <= 0)
                {
                    mes = "错误，传入的RFID为空，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                //单个码不判断重复
                if (Rfid_value[0] == string.Empty || Rfid_value[1] == string.Empty)
                {
                    mes = "有码为空不判断重复";
                    RFIDLogInfo(mes);
                    return true;
                }

                //前缀匹配
                string Date_str = Rfid_value[0].Substring(0, 14);
                string Date_str2 = Rfid_value[1].Substring(0, 14);
                RFIDLogInfo("A穴位：" + Date_str);
                RFIDLogInfo("B穴位：" + Date_str2);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，穴位A:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!Regex.IsMatch(Date_str2, parrern1))
                {
                    mes = "错误，穴位B:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //检查2个RFID是否重复
                if (Rfid_value[0] == Rfid_value[1])
                {
                    mes = "错误，穴位A与B:RFID重复，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //检查2个RFID是否重复
                string str1 = "";
                string str2 = "";
                if (Rfid_value[0] != string.Empty)
                    str1 = Rfid_value[0].Substring(3, 1);
                if (Rfid_value[0] != string.Empty)
                    str2 = Rfid_value[1].Substring(3, 1);
                if (Rfid_value[0] != string.Empty && str1 != "A")
                {
                    mes = "错误，穴位A：字母不匹配或为反异常，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (Rfid_value[1] != string.Empty && str2 != "B")
                {
                    mes = "错误，穴位B：字母不匹配或为反异常，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                mes = "";
                RFIDLogInfo("重码校验成功。A_RFID" + Rfid_value[0]);
                RFIDLogInfo("重码校验成功。B_RFID" + Rfid_value[1]);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误,重码校验失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }
        private bool Check4(string[] Rfid_value, out string mes)
        {
            try
            {
                if (Rfid_value == null || Rfid_value.Length <= 0)
                {
                    mes = "错误，传入的RFID为空，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                //单个码不判断重复
                if (Rfid_value[0] == string.Empty || Rfid_value[1] == string.Empty)
                {
                    mes = "有码为空不判断重复";
                    RFIDLogInfo(mes);
                    return true;
                }

                //前缀匹配
                string Date_str = Rfid_value[0].Substring(0, 14);
                string Date_str2 = Rfid_value[1].Substring(0, 14);
                RFIDLogInfo("A穴位：" + Date_str);
                RFIDLogInfo("B穴位：" + Date_str2);
                var parrern1 = @"^[A-Z]\d{2}[AB]\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4}$";
                if (!Regex.IsMatch(Date_str, parrern1))
                {
                    mes = "错误，穴位A:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }
                if (!Regex.IsMatch(Date_str2, parrern1))
                {
                    mes = "错误，穴位B:RFID前缀不符合规则，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                //检查2个RFID是否重复
                if (Rfid_value[0] == Rfid_value[1])
                {
                    mes = "错误，穴位A与B:RFID重复，请检查。";
                    RFIDLogInfo(mes);
                    return false;
                }

                ////检查2个RFID是否重复
                //string str1 = "";
                //string str2 = "";
                //if (Rfid_value[0] != string.Empty)
                //    str1 = Rfid_value[0].Substring(3, 1);
                //if (Rfid_value[0] != string.Empty)
                //    str2 = Rfid_value[1].Substring(3, 1);
                //if (Rfid_value[0] != string.Empty && str1 != "A")
                //{
                //    mes = "错误，穴位A：字母不匹配或为反异常，请检查。";
                //    RFIDLogInfo(mes);
                //    return false;
                //}
                //if (Rfid_value[1] != string.Empty && str2 != "B")
                //{
                //    mes = "错误，穴位B：字母不匹配或为反异常，请检查。";
                //    RFIDLogInfo(mes);
                //    return false;
                //}

                mes = "";
                RFIDLogInfo("重码校验成功。A_RFID" + Rfid_value[0]);
                RFIDLogInfo("重码校验成功。B_RFID" + Rfid_value[1]);
                return true;
            }
            catch (Exception er)
            {
                mes = "错误,重码校验失败。" + er.Message + "; " + er.ToString() + "; " + er.StackTrace;
                RFIDLogInfo(mes);
                return false;
            }
        }
        private int GetStationLength(string Station)
        {
            try
            {
                switch (Station)
                {
                    case "P1": return 16;
                    case "P2": return 18;
                    case "P3": return 20;
                    case "P4": return 22;
                    case "P5": return 24;
                    case "P6": return 26;
                    case "P7": return 28;
                    case "P8": return 30;
                    case "P9": return 32;
                    case "P10": return 34;
                    default: return 0;
                }
            }
            catch (Exception)
            {
                return 0;
            }

        }
        private string GetStation(int pos)
        {
            try
            {
                switch (pos)
                {
                    case 0: return "P1";
                    case 2: return "P2";
                    case 4: return "P3";
                    case 6: return "P4";
                    case 8: return "P5";
                    case 10: return "P6";
                    case 12: return "P7";
                    case 14: return "P8";
                    case 16: return "P9";
                    case 18: return "P10";
                    default: return "";
                }
            }
            catch (Exception)
            {
                return "";
            }

        }

        public double DiffDays(DateTime startTime, DateTime endTime)
        {
            TimeSpan daysSpan = new TimeSpan(endTime.Ticks - startTime.Ticks);
            return daysSpan.TotalDays;
        }

        #endregion


        #region 信息

        static object logInfoLock = new object();
        static string path = "D://Logs/RFIDLog/";
        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="strMessage">信息</param>
        private static void RFIDLogInfo(string strMessage)
        {
            try
            {
                lock (logInfoLock)
                {
                    strMessage = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "->" + strMessage.Replace("\r\n", "");
                    strMessage += "\r\n";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    StreamWriter LogSw = new StreamWriter(path + DateTime.Today.ToString("yyyy-MM-dd") + ".txt", true, Encoding.GetEncoding(0));
                    LogSw.WriteLine(strMessage);
                    LogSw.Flush();
                    LogSw.Close();
                    LogSw.Dispose();
                }
            }
            catch
            {

            }
        }
        #endregion


        #region
        public void Serialize<T>(T source)//序列化方法
        {
            if (!typeof(T).IsSerializable)
            {
                throw new ArgumentException("The type must be serializable.", "source");
            }

            if (Object.ReferenceEquals(source, null))
            {
                return;
            }

            using (FileStream fs = new FileStream("D:\\Config\\Person.adc", FileMode.Create))//创建一个文件流，对文件进行写入
            {
                BinaryFormatter bf = new BinaryFormatter();//使用CLR二进制格式器
                bf.Serialize(fs, source); //序列化到硬盘
                fs.Close();//关闭文件流
            }
        }

        public T Deserialize<T>()//反序列
        {

            using (FileStream fs = new FileStream("D:\\Config\\Person.adc", FileMode.Open))
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                return (T)binaryFormatter.Deserialize(fs);
            }
        }

        public T Deserialize2<T>()//夸集合反序列
        {

            using (FileStream fs = new FileStream("D:\\Config\\Person.adc", FileMode.Open))
            {
                IFormatter binaryFormatter = new BinaryFormatter();
                binaryFormatter.Binder = new UBinder();
                return (T)binaryFormatter.Deserialize(fs);
            }
        }

        public class UBinder : SerializationBinder
        {
            public override Type BindToType(string assemblyName, string typeName)
            {
                Assembly ass = Assembly.GetExecutingAssembly();
                return ass.GetType(typeName);
            }
        }

        #endregion

    }

}

